package com.jhg.drgclinicalpathway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jhg.drgclinicalpathway.dao.*;
import com.jhg.drgclinicalpathway.domain.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * v1.0
 * 成本管理模块接口
 * by WY
 */
@Api("成本管理")
@RestController
@RequestMapping("Cost")
@Slf4j(topic = "CostController")
public class CostController {
    @Autowired
    V_C成本项目目录Dao hisdmmlDao;
    @Autowired
    Yb_c项目成本Dao ybC成本项目Dao;
    @Autowired
    VDCP_入组结算明细Dao rzjsmxDao;
    @PostMapping("sfxm-ls")
    public RpnMsg costLs(@RequestBody Map<String,Object> map){
        int lb =(int) map.get("lb");
        if(lb==1) {
            int page = (int) map.get("page");
            int size = (int) map.get("size");
            String keys = (String) map.get("keys");
            String xmlb = (String) map.get("fylb");// 增加按收费项目类别筛选
            Page<V_C成本项目目录> pageInfo = new Page<V_C成本项目目录>(page, size);
            QueryWrapper<V_C成本项目目录> queryWrapper = new QueryWrapper<V_C成本项目目录>();

            if(!StringUtils.isEmpty(keys)){
                queryWrapper.and(
                        wrapper ->
                                wrapper.like("ybbm", keys)
                                        .or().like("bm", keys)
                                        .or().like("mc", keys)
                );
            }
            if(!StringUtils.isEmpty(xmlb)){
                queryWrapper.eq("lb",xmlb);
            }
            // zt=1 停用时间大于当前时间
            // queryWrapper.eq()
            IPage<V_C成本项目目录> ls = hisdmmlDao.selectPage(pageInfo, queryWrapper);
            return RpnMsg.SUCCESS(ls);
        } else if(lb==2) {
            return RpnMsg.SUCCESS(hisdmmlDao.getCostFYLB());
        } else if(lb==3) {
            String kssj = map.get("kssj")+" 00:00:01";
            String jssj = map.get("jssj")+" 23:59:59";
            int size = (int) map.get("size");
            return RpnMsg.SUCCESS(hisdmmlDao.getCostCBHZ(size, kssj, jssj));
        } else if(lb==4) {
            String kssj = map.get("kssj")+" 00:00:01";
            String jssj = map.get("jssj")+" 23:59:59";
            String bzbm = map.get("bzbm").toString();
            return RpnMsg.SUCCESS(hisdmmlDao.getCostCBHZ_HZmx(bzbm, kssj, jssj));
        } else if(lb==5) {
            String jzh = map.get("jzh").toString();
            return RpnMsg.SUCCESS(hisdmmlDao.getCostCBHZ_mx(jzh));
        }
        return RpnMsg.SUCCESS();
    }

    @PostMapping("Save-CostRef")
    public RpnMsg SaveCostRef(@RequestBody Map<String,Object> map) {
        System.out.println(map);
        String sfxmid=map.get("sfxmid").toString();
        if(StringUtils.isEmpty(sfxmid)) {
           return RpnMsg.FAIL("收费项目不能为空，请选择项目后再设置成本");
        }
        QueryWrapper<Yb_c项目成本> queryWrapper = new QueryWrapper<Yb_c项目成本>();
        queryWrapper.eq("sfxmid",map.get("sfxmid").toString());
        List<Yb_c项目成本> ls = ybC成本项目Dao.selectList(queryWrapper);
        Yb_c项目成本 yc = new Yb_c项目成本();
        if(ls.size()>0) {
            Yb_c项目成本 cyc = (Yb_c项目成本) ls.get(0);
            yc.setId(cyc.getId());
        }
        yc.setSfxmid(map.get("sfxmid").toString());
        yc.setSfxmbm(map.get("sfxmbm").toString());
        yc.setFy1(Double.parseDouble(map.get("fy1").toString()));
        yc.setFy2(Double.parseDouble(map.get("fy2").toString()));
        yc.setFy3(Double.parseDouble(map.get("fy3").toString()));
        yc.setFy4(Double.parseDouble(map.get("fy4").toString()));
        yc.setFy5(Double.parseDouble(map.get("fy5").toString()));
        yc.setFy6(Double.parseDouble(map.get("fy6").toString()));
        yc.setCjsj(new Date());
        yc.setJlry(map.get("czry").toString());
        if(yc.getId()>0) {
            ybC成本项目Dao.updateById(yc);
        } else {
            ybC成本项目Dao.insert(yc);
        }
        return RpnMsg.SUCCESS();
    }
}
